using System.Collections.Generic;
using System.Linq;

namespace Flood.FloodStrategies
{
	/// <summary>
	/// on the sample set
	/// 23203 in 1.984 seconds
	/// </summary>
	public sealed class FloodAdjacentColorThatHasTheMostDryShapes : FloodStrategyBase, IFloodStrategy
	{
		#region IFloodStrategy Members

		public Result GetNextToFlood(IEnumerable<Shape> moistShapes, Grid grid)
		{
			var first = moistShapes
				.Select(shape => new { Count = shape.DryAdjacentShapes.Count(), shape.Color })
				.OrderByDescending(item => item.Count)
				.First();
			if (first.Count == 0)
			{
				// there are no more blocked squares so the solution
				// order for the remaining squares doesn't matter
				return new Result(moistShapes.Select(shape => shape.Color).OrderByDescending(x => x).ToList(), true);
			}
			return new Result(first.Color, false);
		}

		#endregion
	}
}